package com.catmiao.ai.config;

import com.catmiao.ai.service.ChatAssistant;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LLMConfig {

    @Bean
    public ChatModel embeddingModel(){

        return OpenAiChatModel.builder()
                .apiKey("sk-7217c7d02f0a445993b1d2e7b6dfc10c")
                .modelName("qwen-plus") // 文本向量化模型
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logResponses(true)
                .logRequests(true)
                .build();
    }


    /**
     * 基于内存的向量数据库
     * @return
     */
    @Bean
    public InMemoryEmbeddingStore<TextSegment> embeddingStore(){
        return new InMemoryEmbeddingStore();
    }



    @Bean
    public ChatAssistant assistant(ChatModel chatModel,EmbeddingStore<TextSegment> embeddingStore){

        return AiServices.builder(ChatAssistant.class)
                .chatModel(chatModel)
                .chatMemory(MessageWindowChatMemory.withMaxMessages(50))
                .contentRetriever(EmbeddingStoreContentRetriever.from(embeddingStore))
                .build();
    }
}
